﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Common;

namespace AutoPartsSearch.Web.Infrastructure.Cache.Repositories
{
    public class DictionaryRepository : IDictionaryRepository
    {
        private readonly IDictionaryRepository _dbReposiotry;
        private readonly int _timeToHoldCache = 5;
        private const string ClassKey = "AutoPartsSearch.Web.Infrastructure.Cache.Repositories.DictionaryRepository";

        public DictionaryRepository(IConfigurationGetter configurationGetter, IDictionaryRepository dbRepository)
        {
            _dbReposiotry = dbRepository;
            int value;
            if (int.TryParse(configurationGetter.GetConfigurationValue(Constants.DictionaryCacheInMinutes), out value))
            {
                this._timeToHoldCache = value;
            }
        }

        public IEnumerable<Domain.Entities.Dictionary> GetByParentId(int parentId)
        {
            return CacheExecutor.Execute(() => _dbReposiotry.GetByParentId(parentId),
                _timeToHoldCache, ClassKey, parentId, "GetByParentId");
        }

        public IEnumerable<Domain.Entities.Dictionary> GetByType(Domain.Entities.Enums.DictionaryType type)
        {
            return CacheExecutor.Execute(() => _dbReposiotry.GetByType(type),
                _timeToHoldCache, ClassKey, type, "GetByType");
        }

        public IEnumerable<Domain.Entities.Dictionary> GetByParentType(Domain.Entities.Enums.DictionaryType type)
        {
            return CacheExecutor.Execute(() => _dbReposiotry.GetByParentType(type),
                _timeToHoldCache, ClassKey, type, "GetByParentType");
        }

        public IEnumerable<Domain.Entities.Dictionary> GetByAdditionalParentType(Domain.Entities.Enums.DictionaryType type)
        {
            return CacheExecutor.Execute(() => _dbReposiotry.GetByAdditionalParentType(type),
                _timeToHoldCache, ClassKey, type, "GetByAdditionalParentType");
        }

        public IEnumerable<Domain.Entities.Dictionary> GetByDictionaryTypeParent(Domain.Entities.Enums.DictionaryType type)
        {
            return CacheExecutor.Execute(() => _dbReposiotry.GetByDictionaryTypeParent(type),
                _timeToHoldCache, ClassKey, type, "GetByDictionaryTypeParent");
        }

        public int Add(Domain.Entities.Dictionary instance)
        {
            return _dbReposiotry.Add(instance);
        }

        public void Update(Domain.Entities.Dictionary instance)
        {
            _dbReposiotry.Update(instance);
        }

        public Domain.Entities.Dictionary GetById(int id)
        {
            return CacheExecutor.Execute(() => _dbReposiotry.GetById(id),
                _timeToHoldCache, ClassKey, id, "GetById");
        }

        public IEnumerable<Domain.Entities.Dictionary> Get()
        {
            return CacheExecutor.Execute(() => _dbReposiotry.Get(),
                _timeToHoldCache, ClassKey, "Get");
        }

        public IEnumerable<Domain.Entities.Dictionary> Get(int page, int pageSize)
        {
            return CacheExecutor.Execute(() => _dbReposiotry.Get(page, pageSize),
                _timeToHoldCache, ClassKey, page, pageSize, "Get");
        }

        public void Delete(int id)
        {
            _dbReposiotry.Delete(id);
        }

        public long GetTotalCount()
        {
            return CacheExecutor.Execute(() => _dbReposiotry.GetTotalCount(),
                _timeToHoldCache, ClassKey, "GetTotalCount");
        }
    }
}